package com.cskaoyan.service.admin;

//import com.cskaoyan.bean.MarketStorage;
//import com.cskaoyan.mapper.MarketStorageMapper;
//import com.cskaoyan.utils.FileNameUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.io.IOException;
//import java.sql.SQLOutput;
//import java.util.Date;
//import java.util.Objects;
//
///**
// * @Description
// * @Author klw
// * @Data 2022/11/19 下午9:45
// */
//
//@Service
//public class StorageServiceImpl implements StorageService {
//    @Autowired
//    MarketStorageMapper marketStorageMapper;
//
//    @Value("${file.path}")
//    String filePath;
//
//    @Override
//    public MarketStorage uploadFile(MultipartFile file) throws IOException {
//        String type = file.getContentType(); //获取类型
//
//        String originalFilename = file.getOriginalFilename();//获取文件名
//        long size = file.getSize(); //文件size
//        Date addTime = new Date();
//        Date updateTime = new Date(); //update待优化
//
//        originalFilename = Objects.requireNonNull(originalFilename).toLowerCase();
//        //获取图片类型
//        String suffixName = FileNameUtils.getSuffix(originalFilename);
//        //uuid--文件名
//        String key = FileNameUtils.getFileName(originalFilename);
//
//        file.transferTo(new File(filePath + key));
//
//
//        //String url = filePath + key;
//         String url = "http://localhost:8083/pic/" + key;
//        MarketStorage marketStorage = new MarketStorage(null, key, originalFilename, type, size, url, addTime, updateTime, null);
//
//        marketStorageMapper.insert(marketStorage);  // todo: try-catch
//
//        return marketStorage;
//    }
//}

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.config.AliyunComponent;
import com.cskaoyan.config.Oss;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;


/**
 * @author zjq
 * @date 2022/11/20 15:16
 */
@Component
public class StorageServiceImpl implements StorageService {

    @Autowired
    AliyunComponent aliyunComponent;


    @Autowired
    MarketStorageMapper marketStorageMapper;

    /**
     * @author zjq
     * 文件上传，返回值为文件上传后的信息
     * @date 2022/11/20 15:16
     */
    @Override
    public MarketStorage fileUpload(MultipartFile multipartFile) throws IOException {
//        System.out.println("multipartFile.getOriginalFilename() = " + multipartFile.getOriginalFilename());
//        System.out.println("multipartFile.getContentType() = " + multipartFile.getContentType());
//        System.out.println("multipartFile.getBytes().length = " + multipartFile.getBytes().length);
//        System.out.println("multipartFile.getName() = " + multipartFile.getName());

        String fileName = multipartFile.getOriginalFilename();
        Integer size = multipartFile.getBytes().length;
        InputStream inputStream = multipartFile.getInputStream();
        String key = UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
        aliyunComponent.fileUpload(key,inputStream);
        inputStream.close();
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setAddTime(new Date());
        marketStorage.setSize((long)size);
        marketStorage.setName(key);
        marketStorage.setDeleted(false);
        marketStorage.setType(multipartFile.getContentType());
        Oss oss = aliyunComponent.getOss();
        marketStorage.setUrl("http://"+oss.getBucket()+"."+oss.getEndPoint()+"/"+key);
        marketStorageMapper.insertNewFile(marketStorage);
        int i = marketStorageMapper.selectIdbyKey(marketStorage.getName());
        marketStorage.setId(i);
        return marketStorage;
    }

    @Override
    public CommonData getList(Integer page, Integer limit, String key, String name) {

        PageHelper.startPage(page, limit);
        MarketStorageExample example = new MarketStorageExample();
//        example.setDistinct(true);


        MarketStorageExample.Criteria criteria = example.createCriteria();

        if (key != null && !"".equals(key)) {//StringUtils isEmpty
            criteria.andKeyLike("%"+key+"%");
        }
        if (name != null && !"".equals(name)) {

            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(marketStorages);
        CommonData commonData = new CommonData();
        commonData.setLimit(limit);
        commonData.setPage(pageInfo.getPageNum());
        commonData.setTotal((int) pageInfo.getTotal());
        commonData.setPages(pageInfo.getPages());
        commonData.setList(marketStorages);
        return commonData;
    }

    @Override
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        marketStorage.setUpdateTime(new Date());
        int i = marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        if(i==1){
            return marketStorage;
        }
        return null;
    }

    @Override
    public int deleteStorage(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        int i = marketStorageMapper.updateByPrimaryKeySelective(marketStorage);
        return i;
    }
}

